package com.test.code;


import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CodeGenerator {
    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java"); //生成文件输出目录
        //根据实际配置

        gc.setAuthor("lihaozhe");
        gc.setOpen(false);
        // gc.setSwagger2(true); 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();

		dsc.setUrl("jdbc:postgresql://127.0.0.1:5432/mining?useSSL=false&characterEncoding=utf-8&rewriteBatchedStatements=true&autoReconnect=true&useServerPrepStmts=false&stringtype=unspecified&currentSchema=livewall");
        dsc.setDriverName("org.postgresql.Driver");
        dsc.setSchemaName("public");
        dsc.setUsername("postgres");
        dsc.setPassword("pg-dev123*");
//        dsc.setSchemaName("working");

//        dsc.setUrl("jdbc:oracle:thin:@10.10.15.57:1521:orcl");
//        dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
//        dsc.setUsername("szalertdb");
//        dsc.setPassword("Szalertdb");
//        mpg.setDataSource(dsc);

//        dsc.setUrl("jdbc:oracle:thin:@192.168.0.39:1522:orcl");
//        dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
//        dsc.setUsername("SZREST");
//        dsc.setPassword("SZREST");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();;
        pc.setParent("com.bdtd.backend");
        pc.setService("service.pg");
        pc.setMapper("mapper.pg");
        pc.setServiceImpl("service.pg.impl");
        mpg.setPackageInfo(pc);
        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return projectPath + "/src/main/resources/mapper/pg/tunnel/" +"/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);

        mpg.setCfg(cfg); //这个必须要,需要提供一个默认的
        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        // 关闭 默认 xml文件生成
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);//表名生成策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//实体字段生成策略
		strategy.setInclude(scanner("请输入需要生成的表名").split(",")); //需要生成的表

        //strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        strategy.setEntityLombokModel(true);//使用lombook
        strategy.setRestControllerStyle(true);
        // 公共父类
        //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        // 写于父类中的公共字段
//        strategy.setSuperEntityColumns("id");
//        strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
//        strategy.setControllerMappingHyphenStyle(true);
//        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
